'use strict';
var _ = require('lodash');
//////////////////////////////////////////////////////////////////////////////
// 提交按钮控件
// =====================
// liuyp 2018年11月28日15: 30: 04
//////////////////////////////////////////////////////////////////////////////
function ngSubmitBtn($http,$timeout) {
    function A(ele){
        this.target = null
        this.timer = 0
        this.loadingtext = "中 <div class=loadingicon></div>"
        this.disable = function(){
            this.target.attr("disabled" , true)
            this.target.addClass("loadingbtn")
            this.text = this.target.html()
            this.target.html(this.text + this.loadingtext)
            var self = this
            this.timer = setTimeout(function(){ self.enable.call(self) }, 10e3)
        }
        this.enable = function(){
            this.target.removeAttr("disabled")
            this.target.removeClass("loadingbtn")
            this.target.html(this.text)
            clearTimeout(this.timer)
            this.timer = 0
        }
        this.target = ele
    }
    return {
        restrict: 'A',
        link: function(scope, ele, attrs) {
            var submitBtn = new A(ele)
            var throttleFun = _.throttle(function(){
                submitBtn.disable();// 禁用提交按钮
                var fun = attrs.ngSubmitBtn.split("(")
                scope.imformimsubmiting = function(){
                    try{
                        Array.prototype.push.call(arguments,submitBtn)
                        scope.$eval(fun[0]).apply(null,arguments)
                    }catch(e){
                        console.error('ngSubmitBtn throw big error ~', e)
                    }
                    scope.$apply()
                }
                scope.$eval("imformimsubmiting("+fun[1])
            }, 1e3, {'trailing':false})
            ele.bind("click", throttleFun)
        }
    }
}
exports = module.exports = ngSubmitBtn;